<style lang="less">
  @import './login.less';
</style>

<template>
  <div class="login" style="background:#f5f7f9">
    <div class="login">
      <div class="login-con">
        <img src="../../assets/images/logo-black.png" draggable="false" width="100%" />
        <div class="form-con">
          <login-form @on-success-valid="handleSubmit" :loading='loading'></login-form>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
  import LoginForm from '@/components/login-form'
  import store from '@/store'
  import errHandler from '@/libs/err-handler';
  import tools from '@/libs/tools.js';
  import Cookies from 'js-cookie'
  import vueQr from 'vue-qr'

  import {
    mapActions
  } from 'vuex'
  import {
    initRouter,
  } from '@/libs/router-util'
  import {
    localSave,
  } from '@/libs/util'
  export default {
    components: {
      LoginForm,
      vueQr
    },
    data() {
      return {
        loading: false,
        uuid: null,
        imageUrl: require("../../assets/images/logo-min.png"),
      }
    },
    created() {

    },
    methods: {
      ...mapActions([
        'handleLogin',
        'getUserInfo'
      ]),
      mockLogin() {
        let tokendata = {
          "token": "admin"
        }
        store.commit('setToken', tokendata.token)
        setTimeout(() => {
          let res = {
            "data": {
              "enabled": "1",
              "mobilePhone": "138",
              "phDevopsAccountId": 0,
              "realname": "测试",
              "username": "1388"
            },
            "status": 200
          }
          let data = res.data;
          store.commit('setAvator', data.avator)
          store.commit('userInfo', res.data);
          localSave("ls-health-devops-web", JSON.stringify(res.data));
          store.commit('setAccess', ["admin"]);
          store.commit('setHasGetInfo', true);
        }, 300);
        setTimeout(() => {
          initRouter()
          this.$router.push({
            name: this.$config.homeName
          })
        }, 600)
      },
      handleSubmit({
        userName,
        password
      }) {
        let info = {
          "transId": this.uuid,
          "action": "machineCode",
        }
        this.mockLogin()
        this.loading = true;
        this.login(userName, password);
      },
      login(userName, password) {
        this.handleLogin({
          userName,
          password,
        }).then(result => {
          this.loading = false;
          let headers = {
            headers: {
              'Authorization': 'Bearer ' + result.access_token,
            }
          }
          this.getUserInfo().then(data => {
            initRouter()
            this.$router.push({
              name: this.$config.homeName
            })
          }).catch(() => {
            this.loading = false;
            this.$msg.error.call(this, '登录失败-获取个人信息失败');
          })

        }).catch(err => {
          this.loading = false;
          console.log('登录失败: ' + err)
          this.$msg.error.call(this, err ? err : '登录失败')
        })
      }
    }
  }
</script>

<style>

</style>